package com.pratik.gedcom.domain;

import java.util.ArrayList;
import java.util.List;

public class Gedcome {

	private String tag;
	private String value;
	private List<Gedcome> subGedcomeList = new ArrayList<Gedcome>();
	private Integer level;
	private String id;

	public String getTag() {
		return tag;
	}

	protected List<Gedcome> getSubGedcomeList() {
		return subGedcomeList;
	}

	public String getData() {
		return value;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public void setSubGedcomeList(List<Gedcome> subGedcomeList) {
		this.subGedcomeList = subGedcomeList;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Integer getLevel() {
		return this.level;
	}

	public void addChild(Gedcome childGedcome) {
		Gedcome gedcome = this.getParent(childGedcome);
		gedcome.getSubGedcomeList().add(childGedcome);
	}

	private Gedcome getParent(Gedcome childGedcome) {
		Gedcome parent = this;
		while (!parent.isParentLevelItem(childGedcome.getLevel())) {
			parent = parent.getLastInsertedElementInSubGedcomeList();
		}
		return parent;
	}

	private Gedcome getLastInsertedElementInSubGedcomeList() {
		return this.getSubGedcomeList()
				.get(this.getSubGedcomeList().size() - 1);
	}

	public boolean isParentLevelItem(Integer level) {
		return this.level == (level - 1);
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getXMLString() {
		return createStartTag() + getNonAttributedDataValue()
				+ (!this.isLeafGedcom() ? "" : getChildGedcomesString())
				+ createEndTag();
	}

	private String getChildGedcomesString() {
		String xmlString="";
		for (int i=0;i < subGedcomeList.size();i++) {
			xmlString=xmlString+subGedcomeList.get(i).getXMLString();
		}
		return xmlString;
	}

	

	private String createEndTag() {
		return "</" + tag + ">";
	}

	private String createStartTag() {
		return "<" + tag + getAttributedId() + getAttributedDataValue() + ">";
	}

	private String getAttributedId() {
		return id == null ? "" : getIdAsAttribute();
	}

	private String getNonAttributedDataValue() {
		String valueValue = "";
		if (!isLeafGedcom()) {
			valueValue = (value == null) ? "" : getData().replace("&", "&amp").replace("\"", "&quot").replace("\'", "&apos").replace("<", "&lt").replace(">", "&gt");
		}
		return valueValue;
	}

	private String getAttributedDataValue() {
		String attributedDataValue = "";
		if (isLeafGedcom()) {
			attributedDataValue = value==null?"":" value=\"" + getData().replace("&", "&amp").replace("\"", "&quot").replace("\'", "&apos").replace("<", "&lt").replace(">", "&gt") + "\"";
		}
		return attributedDataValue;
	}

	private boolean isLeafGedcom() {
		return subGedcomeList != null && !subGedcomeList.isEmpty();
	}

	public String getIdAsAttribute() {
		return " id=\"" + id + "\"";
	}

	public String getId() {
		return id;
	}

}
